<?php
/**
 * Plugin Notification System.
 *
 * @package WP cr mobile
 */
class CRMobile_Notification
{
    public static function notify($company, $review)
    {
        $data = array(
            'company' => $company->name,
            'rating'  => isset($review['rating']) ? $review['rating'] : 0,
            'review'  => $review['review']
        );

        // Find out who to notify
        if ($company->moderator != 0) {
            $user = get_userdata($company->moderator);
            $to = $user->user_email;
        } else {
            $to = get_option('admin_email');
        }

        // Prepare the email
        $subject = 'A new WP CMob Review submitted';
        $message = CRMobile_View::render(
            plugin_dir_path(__FILE__).'../../views/mail-notification.php',
            $data
        );

        add_filter('wp_mail_content_type', array('CRMobile_Notification', 'set_html_content_type'));
        wp_mail($to, $subject, $message);
        remove_filter('wp_mail_content_type', array('CRMobile_Notification', 'set_html_content_type'));
    }

    public static function mailReviewer($company, $rating, $to)
    {
        // Find out who the email shall be sent from
        if ($company->moderator != 0) {
            $user = get_userdata($company->moderator);
            $from = $user->user_email;
        } else {
            $from = get_option('admin_email');
        }

        $headers = "From: {$company->name} <{$from}>" . "\r\n";
        $subject = 'Thank you for your review';

        $message = CRMobile_Theme::selectText($company->emails, $rating, 'emails_');
        $message = nl2br($message);

        // Add the coupon if it exist.
        if (strlen($company->coupon) > 0) {
            $message .= "<br/><img src='{$company->coupon}' />";
        }

        add_filter('wp_mail_content_type', array('CRMobile_Notification', 'set_html_content_type'));
        wp_mail($to, $subject, $message, $headers);
        remove_filter('wp_mail_content_type', array('CRMobile_Notification', 'set_html_content_type'));
    }

    public static function set_html_content_type()
    {
        return 'text/html';
    }

    public static function mailResponder($company, $mail)
    {
        $headers = "From: {$mail} <{$mail}>" . "\r\n";
        $subject = 'Signup';
        $message = 'Signup';

        switch ($company->optin_service) {
            case 'aweber':
                $to = $company->optin_address.'@aweber.com';
                break;
            case 'getresponse':
                $to = $company->optin_address.'@getresponse.com';
                break;
            case 'other':
                $to = $company->optin_address;
                break;
            default:
                return;
                break;

        }
        wp_mail($to, $subject, $message, $headers);
    }
}
